<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="images/xm.jpeg">
    <title>注册页面</title>
    <style>
        body {
            background: url('images/b.jpg') no-repeat center center fixed;
            background-size: cover;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-module {
            background: rgba(255, 255, 255, 0.8);
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 400px;
            margin: auto;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .login-module h1 {
            color: #007bff;
            font-size: 45px;
            margin-bottom: 20px;
        }
        .login-module input[type="text"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            box-sizing: border-box;
        }
        .login-module input[type="password"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            box-sizing: border-box;
        }
        .login-module button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-bottom: 10px;
        }
        .login-module button:hover {
            background-color: #0056b3;
        }
        .login-module a {
            color: #007bff;
            text-decoration: none;
            transition: color 0.3s;
            margin: 5px 0;
        }
        .login-module a:hover {
            color: #0056b3;
        }
    </style>
</head>
<body>
<div class="login-module">
    <h1>熊猫票务管理系统</h1>
    <input type="text" id="username" placeholder="请输入用户名">
    <input type="password" id="password" placeholder="设置密码">
    <input type="password" id="confirm-password" placeholder="确认密码">
    <input type="text" id="email" placeholder="请输入邮箱">
    <input type="text" id="role_id" placeholder="用户请输入1，管理员请输入2">
    <button type="submit" onclick="register()">注册</button>
    <a id="login" href="/templates/Login.html">已有账号？登录</a>
</div>
<script>
    function register() {
        const username = document.getElementById("username").value;
        const password = document.getElementById("password").value;
        const confirmPassword = document.getElementById("confirm-password").value;
        const email = document.getElementById("email").value;
        const role_id = document.getElementById("role_id").value;

        if (!username || !password || !confirmPassword) {
            alert("请输入用户名和密码，并确认密码");
            return;
        }

        if (password !== confirmPassword) {
            alert("两次输入的密码不一致，请重新输入");
            return;
        }

        // 使用fetch API发送注册信息
        fetch('/user/register', {
            method: "POST",
            headers: {
                "Content-Type": "application/json;charset=UTF-8"
            },
            body: JSON.stringify({ username: username,password: password,email: email,roleId: role_id})
        })
            .then(response => response.text())
            .then(data => {
                let json = JSON.parse(data);
                console.log(json);
                if (json.id === null) {
                    alert("用户名已存在");
                } else {
                    alert("注册成功");
                    window.location.href = '/templates/login.html';
                }
            })
    }

</script>
</body>
</html>
